<?php
namespace app\admin\controller;
use think\Controller;
use think\Db;
use app\common\controller\AdminBaseController;
class UserController extends AdminBaseController
{ 
	/**
     * 管理员管理
     * @return mixed
     */
    public function index() {
	    /** 查询登录数据*/
    	$grab = Db::name('admin_log')->field("add_time,ip,u_id")->order('add_time desc')->limit(1)->buildSql();
    	$auth_group_access=Db::name('auth_group_access')->buildSql();
    	$auth_group=Db::name('auth_group')->buildSql();
		$data=Db::name('auth_user a')
		->join($auth_group_access.'b','a.id=b.uid','LEFT')
		->join($auth_group.'c','b.group_id=c.id','LEFT')
		->join($grab.'d','d.u_id=a.id','LEFT')
		->field('a.admin_name,a.status ,a.id,a.real_name,a.thumb,c.title,c.id pid,d.add_time time,d.ip')
		->select();
		$this->assign('list',Db::name('auth_group')->where('id != 1')->order('id DESC')->select());
        return $this->fetch('index', ['user_list' => $data]);
    }
	
	  /**
     * [rule_state 用户状态]
     * @return [type] [description]
     */
    public function rule_state()
    {
        $id = input('id');
        $status = Db::name('auth_user')->where('id',$id)->value('status');//判断当前状态
        if($status==1)
        {
            if(Db::name('auth_user')->where('id',$id)->update(['status'=>0])){
            	return	toSuccess('已禁用');
            }else{
            	 return toError(408,'更换失败');
            }

        }
        else
        {
            if(Db::name('auth_user')->where('id',$id)->update(['status'=>1])){
            	return	toSuccess('开启中');
            }else{
            	 return toError(408,'更换失败');
            }
        }
    
    }
    /**
     * [roleDel 删除用户]
     * @return [type] [description]
     */
    public function del_rule()
    {
        $id = input('param.id');
        if (Db::name('auth_user')->where('id',$id)->delete()) {
           return	toSuccess('删除成功');
        } else {
            return toError(408,'删除失败，请重新删除');
        }
    }
    /**
     * [edit_rule 编辑用户]
     * @return [type] [description]
     */
    public function edit_rule($id)
    {
        if(input('post.')){
        	$id=input('id');
            $d=Db::name('auth_user')->find($id);
            if(input('admin_pwd') != $d['admin_pwd']){
            	$param['admin_pwd'] = md5_salt(input('admin_pwd'));
            }
            $group_id=input('pid');
            $param['admin_name']=input('admin_name');
            $param['real_name']=input('real_name');
            $param['status']=input('status');
            $param['thumb']=input('thumb');
             $auth_user=Db::name('auth_user')->find($id);
             if($param['thumb']!=$auth_user['thumb']){
             	$param['thumb']='/public/uploads/face/'.$param['thumb'];
             }
            
            
            if(Db::name('auth_group_access')->where('uid',$id)->find()){
            	$ok1=Db::name('auth_group_access')->where('uid',$id)->update(['group_id'=>$group_id]);
   		 		$ok2=Db::name('auth_user')->where('id',$id)->update($param);
            }else{
            	Db::name('auth_group_access')->insert(['uid'=>$id,'group_id'=>$group_id]);
            }
   		 
            if($ok1 == 1 || $ok2 == 1){
            	return	toSuccess('修改成功');
            }else{
            	 return toError(408,'没有修改，请修改后提交');
            }
        }
 
		$group_id=Db::name('auth_group_access')->where('uid',$id)->find();
        $this->assign('list',Db::name('auth_group')->select());
        $this->assign('group_id',$group_id['group_id']);
        $this->assign('user_list',Db::name('auth_user')->where('id',$id)->find());
        return $this->fetch();
    }
    
  //添加后台用户  
   	public function add_rule(){
		if(input('post.')){
            $param = input('post.');
            $group_id=$param['pid'];
            $param['admin_pwd']=md5_salt($param['admin_pwd']);
            unset($param['pid']);
            unset($param['file']);
             $a=$param['thumb'];
            $param['thumb']='/public/uploads/face/'.$a;
            if(Db::name('auth_user')->insert($param)){
            	$uid = Db::name('auth_user')->getLastInsID();
            		if(Db::name('auth_group_access')->insert(['uid'=>$uid,'group_id'=>$group_id])){
            			return	toSuccess('添加成功');
            		}else{
            					return toError(408,'添加失败，请重新添加');
          				  }
            	
           	 	}else{
            	return toError(408,'添加失败，请重新添加');
            }
            
        }
        $this->assign('list',Db::name('auth_group')->select());
        return $this->fetch('add');
		
	} 
     public function select_role(){
    
        return	json(['code'=>200,'data'=>$auth_rule_list]);
    }
}
?>